using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FinlogiK.FxCop.Statistics.Windows
{
	internal partial class ProjectsForm : ListForm
	{
		#region Constructors

		internal ProjectsForm()
		{
			InitializeComponent();

			this.projectsTableAdapter.Fill(this.projectDataSet.Projects, null);
			this.SetGridBindingSource(this.projectDataSet.Projects);
		}

		#endregion

		#region Methods

		protected override DataGridViewColumn[] GetGridColumns()
		{
			DataGridViewColumn[] columns = new DataGridViewColumn[7];

			columns[0] = this.MakeTextBoxColumn("Name", "ProjectName", true);
			columns[1] = this.MakeCheckBoxColumn("Active?", "IsActive");
			columns[2] = this.MakeCheckBoxColumn("Treat all exclusions as TODOs?", "TreatAllExclusionsAsTODOs");
			columns[3] = this.MakeTextBoxColumn("FxCop project path", "ProjectPath");
			columns[4] = this.MakeButtonColumn(string.Empty, "...");
			columns[5] = this.MakeTextBoxColumn("FxCopCmd.exe path", "FxCopCmdPath");
			columns[6] = this.MakeButtonColumn(string.Empty, "...");

			return columns;
		}

		protected override void SaveData()
		{
			this.projectsTableAdapter.Update(this.projectDataSet.Projects);
		}

		protected override void MainGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			base.MainGrid_CellContentClick(sender, e);

			if (e.ColumnIndex == (int)Column.ProjectPathBuilder)
			{
				this.SelectProjectPath(this.GetGridRow(e.RowIndex));
			}
			else if (e.ColumnIndex == (int)Column.FxCopCmdPathBuilder)
			{
				this.SelectFxCopCmdPath(this.GetGridRow(e.RowIndex));
			}
		}

		private void SelectProjectPath(DataGridViewRow row)
		{
			string path = string.Empty;
			object untypedPath = row.Cells[(int)Column.ProjectPath].Value;
			if (untypedPath != DBNull.Value) path = ((string)untypedPath).Trim();

			using (OpenFileDialog dialog = new OpenFileDialog())
			{
				dialog.CheckFileExists = true;
				dialog.CheckPathExists = true;
				dialog.DefaultExt = "FxCop";
				dialog.DereferenceLinks = true;
				dialog.FileName = path;
				dialog.Filter = "FxCop project files (*.FxCop)|*.FxCop|All files (*.*)|*.*";
				dialog.Title = "Select the FxCop project file...";

				if (dialog.ShowDialog(this) == DialogResult.OK)
				{
					row.Cells[(int)Column.ProjectPath].Value = dialog.FileName.Trim();
				}
			}
		}

		private void SelectFxCopCmdPath(DataGridViewRow row)
		{
			string path = string.Empty;
			object untypedPath = row.Cells[(int)Column.FxCopCmdPath].Value;
			if (untypedPath != DBNull.Value) path = ((string)untypedPath).Trim();

			using (OpenFileDialog dialog = new OpenFileDialog())
			{
				dialog.CheckFileExists = true;
				dialog.CheckPathExists = true;
				dialog.DefaultExt = "exe";
				dialog.DereferenceLinks = true;
				dialog.FileName = path;
				dialog.Filter = "FxCopCmd.exe|FxCopCmd.exe|All executables (*.exe)|*.exe";
				dialog.Title = "Locate FxCopCmd.exe...";

				if (dialog.ShowDialog(this) == DialogResult.OK)
				{
					row.Cells[(int)Column.FxCopCmdPath].Value = dialog.FileName.Trim();
				}
			}
		}

		#endregion

		#region Nested types

		private enum Column
		{
			ProjectName = 0,
			IsActive,
			TreatAllExclusionsAsTODOs,
			ProjectPath,
			ProjectPathBuilder,
			FxCopCmdPath,
			FxCopCmdPathBuilder
		}

		#endregion
	}
}